Okay.
In this lecture, we're going to be filling out the play sounds handler along with our lightning module
script on the client.
Now, the purpose of this module script is to allow the server to tell specific players or all the players
to play a sound on their end.
It gives the server more fine tuned control over the sounds that a player needs to play on their end,
and we're going to be using the same audio script in the main story part of our game, and the one we're
going to script here is going to serve as the foundation for that other script.
So let's go ahead and get started in here.
All we're going to need is a reference to replicated storage.
And then we're going to need a reference to the sound service.
And then I'm going to create the table that's going to represent this module script.
So we'll just call it sounds.
And then we can return sounds at the end of our module script here.
Now our module script is going to do two things.
It's going to listen to an event within replicated storage when the server tells us to play a sound.
And it's also going to have a while loop running here that randomly generates a lightning sounds just
to add more ambience into our game.
Now, in order to be able to reference this lightning module script here, we're going to have to get
this folder or this client folder that actually houses all of the module scripts.
So since our play sounds handler is going to be inside of our handlers folder, we need to get the parent
of this script and then get the parent of this folder to be able to access all of the other module scripts
that are in our game.
So we can create a variable, I'm going to call it main script, and it's going to be equal to this
script parent and get the parent of that parent.
So that way we're referring to this client folder here.
And then we can go ahead and require our lightning module script.
So I can just call this lightning and require from our main script, we'll get the modules folder and
get our lightning module script.
Now currently it's empty but we're going to be filling this out in a moment.
The next thing we need to make a reference to is the play sound event that is inside of replicated storage.
So inside of the events folder and inside of remotes, there is a remote event called Play Sound.
And then lastly inside of the sound service I have a section of all of the different thunder sounds
that we can play.
So there's three different ones, and we're going to randomly pick one of these lightning sounds and
play it.
So I'll create a variable called lightning sounds.
And inside of sound service in the ambience folder get weather and then thunder.
And then we can just get all of those sounds just like this.
And then lastly I'm going to create a new random data type.
Okay, so inside of this service or our handler, we need to have a function to initialize it.
So init.
And then we're going to have a function to start this module.
Inside of this initialize section, all we're going to do is we're going to listen to that play sound
event.
So on client event we're going to connect a function to it.
And what we should get past from the server is a sound instance it wants us to put.
And then we can just go ahead and play that sound just in case the sound doesn't exist.
We can just do, if not sound.
Then we'll just return out of this function.
And then inside of our start function, what we're going to do here is we're going to have a while loop
that's going to use our RNG data type to randomly summon lightning.
Now, because we're going to be using a while loop here, the main thread of execution is going to get
stuck inside of it.
So we need to actually spawn the while loop in another function.
That way, you know, when we're setting up the client, we don't get stuck in this start function here.
So what I could do is I could use task and I could use task dot defer because I don't need this, uh,
to run immediately.
And what we'll do in here is we'll have a while loop that runs forever.
And what we could do is we could use our orange data type.
So if RNG we could do one through 25.
If it's equal to one, then we can go ahead and summon some lightning.
And that's what we'll use our lightning module script for.
So we'll have a function in there to summon lightning.
So let's actually go ahead and fill that out.
So inside of our lightning module script, the only service we're going to need is a reference to the
lightning service.
So game getservice lightning.
And then we'll just call this module script lightning, and we'll make sure to return it at the end
here.
And the only public function we're going to have in here is to summon lightning.
So we could call this function Summon Lightning.
And we can do whatever inside of here.
Now I'm going to create a dedicated function as well for this.
Again I'm going to call it Summon Lightning.
And what we're going to do in here is we're going to use, uh, the random data type again, and set
the brightness in the workspace to random different intervals of numbers.
That way the outside looks like there was a flash of light.
So we need to keep track of the original brightness inside of the lighting service.
So we'll have a variable to do that.
So inside of the lightning service we'll get the original brightness that is in our game.
And then I'm also going to create a variable for a new random data type.
And then inside of here what I'm going to do is I'm going to generate a new random number using RNG.
Next number.
And we can get a random number between 0 and 12.
And this is going to be the brightness we're going to set inside of our game.
So we'll up the brightness anywhere between 8 and 12.
So what we'll do is we'll do lighting service dot brightness is equal to this new random number.
Or actually we can add this to the brightness.
So this new random number we're going to add to the current brightness in our game.
So since the brightness is currently zero what it'll do is it'll pump up the brightness to like eight.
So it looks very bright out like a flash of light just happened.
And then like a little bit of time later we'll drop it down to like three and then go back up to make
it look like it's flickering outside, like there was a lightning strike.
So we can wait a random amount of time, like anywhere between, uh, we could do next number and do
a very short interval of time, like 0.08 seconds or up to 0.12 seconds.
And then we're going to set the brightness in the workspace again.
So instead of adding the random number again, what we could do is we could subtract it by this random
number again.
But I don't want the workspace to go back to its original brightness so we can reduce this number by
something like four.
So first the workspace gets bright and then it gets a bit dimmer.
And then we'll have it get brighter again and then more dimmer.
And then set it back to the original brightness.
So again we can wait for a random amount of time RNG next number.
This time we could probably again wait for 0.08 to 0.12.
And this time what we could do is we could set the brightness in the workspace to something like the
random number, and we can add a number like six, I don't know, just throwing out random numbers.
RNG.
Next number we're going to wait again.
This time we can wait a little bit longer, maybe anywhere from 0.08 seconds to 0.15.
Again, we're going to decrease the brightness in the workspace.
So we could subtract this by like a number like eight.
That way.
At least it's larger than what was added to the brightness.
So we're still dimming the brightness down.
And then again, we're going to wait a random amount of time.
And then finally we can set the lighting service dot brightness back to the original brightness.
So this is just kind of imitating the lightning or the light in our game that would imitate lightning.
And then we can literally just call this function summoned lightning.
All right.
So back to our play sounds handler.
What we can do is we can now call this summon lightning function to flicker the lighting in the workspace.
And then what we're going to do is we're going to actually wait a random amount of time.
So we can use RNG next number and wait anywhere from like 0.1 seconds all the way up to something like
1.5 seconds.
And this is to imitate how long it takes for sound to reach the player after we see the flash of light,
because they're not going to hear the sound instantly if the lightning is super far away.
So we're just going to use this.
Wait here to imitate the sound of, uh, having to travel and then reach the eardrum of our player.
So once we wait that, then we can access our lightning sounds and pick a random one out of there so
we can use RNG next integer and pass one to the max number of lightning sounds that we have, which
would be three.
And we're just going to play a random one of those sounds.
And then after that, we're going to wait for one second in this loop because we don't want to have
a while loop that's running at the speed of light.
Right.
Because if we didn't put this wait statement here, then this if statement is basically useless because
this while loop will be running so fast, that will be eventually guaranteed to hit one.
So we'll put this cooldown in here.
That way lightning isn't playing all of the time, and that's basically all we need to do for our play
sounds handler and our lightning module script.
So if we go and test out our game, actually let me reset the brightness in the workspace back to zero.
If we test this out, then we should get some random lightning playing in our game eventually.
So as you can see, the outside flickered and then we get our lightning.
Click it again outside and got our lightning.
All right so that's all for this lecture.
We're getting pretty close to wrapping up our lobby.
But the bigger project for our game is still ahead of us.
Anyways I'll see you in the next lecture.